package com.paradogs.gs.modules.world.base.unit;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.paradogs.common._cfg.Cfg;
import com.paradogs.gs.modules.Player;
import com.paradogs.gs.modules.WorldObject;
import com.paradogs.gs.modules.WorldSyncData;
import com.paradogs.gs.modules.inventory.InventoryMgr;
import lombok.Data;

import java.util.Map;

/**
 * @author: yumi
 * @date: 2025/4/21  16:08
 * @Description: TODO
 */
@Data
public class WorldDrop extends WorldObject {

    private Cfg.Item template;
    private int count;

    public WorldDrop(Cfg.Item template, int world, int x, int y, int count) {
        this.template = template;
        this.count = count;
        load(
                -IdWorker.getId(),
                world,
                x,
                y,
                count,
                count,
                1,
                1,
                UnitType.DROP
        );
    }

    @Override
    public WorldSyncData toSyncData() {
        return new WorldSyncData(
                getId(),
                template.name(),
                UnitType.DROP,
                template.level(),
                getHp(), // 掉落物血量 = 道具数量
                getMaxHp(),
                getMp(),
                getMaxMp(),
                template.easyDesc(),
                template.detailDesc()
        );
    }

    @Override
    public boolean canBePickUp() {
        return true;
    }

    @Override
    public void BePickUp(WorldObject picker) {
        // 删除掉落
        this.curNode().leave(this);
        // 发奖
        if (picker.getUnitType() == UnitType.PLAYER) {
            WorldPlayer player = (WorldPlayer) picker;
            InventoryMgr mgr = player.getGameObject().getMgr(InventoryMgr.class);
            mgr.update(null, Map.of(template.id(), count));
        }
    }
}
